#!/usr/bin/python
# -*- coding: UTF-8 -*-
import uuid
import hashlib
import time
import config

# 生成MD5编码
def md5vale(key):
    input_name = hashlib.md5()
    input_name.update(key.encode("utf-8"))
    return input_name.hexdigest()


# 拼接天擎URL
def getTqUrl(dic):

    ct = time.time()
    uuid_str = uuid.uuid1()
    timestamp = int(round(ct * 1000))
    httpurl = config.remote_url

    dic['serviceNodeId'] = 'NMIC_MUSIC_CMADAAS'
    dic['userId'] = config.tq_userId
    dic['pwd'] = config.tq_password
    dic['timestamp'] = str(timestamp)
    dic['nonce'] = str(uuid_str)

    url = ''
    pp = ''
    for i in sorted(dic):
        if i == 'pwd':
            pp = '&' + i + '=' + str(dic[i])
        url = url + i + '=' + str(dic[i]) + '&'
    httpurl = httpurl + url

    sign = md5vale(url[0:len(url) - 1])
    sign = sign.upper()

    httpurl = httpurl.replace(pp, '') + 'sign=' + sign
    return httpurl
